package com.dhsc.mybatis.conf;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.logging.nologging.NoLoggingImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName:  ViewDataSourceConfig
 * @Description: 第二数据源配置
 * @date:   2023年10月20日 上午8:30:27       
 * @author: clong
 * @Copyright:  东海运营
 */
@Slf4j
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = ViewDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "viewSqlSessionFactory")
public class ViewDataSourceConfig
{
	static final String PACKAGE = "com.dhsc.view.dao.impl.**";
	
	static final String MAPPER_LOCATION = "classpath:mapper/view/**/*.xml";
	
	@Value("${spring.datasource.view.url}")
	private String url;

	@Value("${spring.datasource.view.username}")
	private String user;

	@Value("${spring.datasource.view.password}")
	private String password;

	@Value("${spring.datasource.view.driverClassName}")
	private String driverClass;
	
	@Value("${spring.datasource.view.initialSize}")
	private int initialSize;
	
	@Value("${spring.datasource.view.minIdle}")
	private int minIdle;
	
	@Value("${spring.datasource.view.maxActive}")
	private int maxActive;
	
	/**
	 * @Title: viewDataSource   
	 * @Description:druid连接池设置 
	 * @return: DataSource      
	 * @throws:SQLException
	 */
	@Bean(name = "viewDataSource")
	@Qualifier("viewDataSource")
	public DataSource viewDataSource() throws SQLException
	{
		try(DruidDataSource ds = new DruidDataSource();){
			ds.setDriverClassName(driverClass);
			ds.setUrl(url);
			ds.setUsername(user);
			ds.setPassword(password);
			ds.setInitialSize(initialSize);
			ds.setMinIdle(minIdle);
			ds.setMaxActive(maxActive);
			
			ds.setMaxWait(60000);
			//配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
			ds.setTimeBetweenEvictionRunsMillis(60000);
			//配置一个连接在池中最小生存的时间，单位是毫秒
			ds.setMinEvictableIdleTimeMillis(300000);
			//打开PSCache，并且指定每个连接上PSCache的大小
			ds.setPoolPreparedStatements(true);
			ds.setMaxPoolPreparedStatementPerConnectionSize(2);
	
			ds.setValidationQuery("SELECT 1  from  DUAL ");
			ds.setTestWhileIdle(true);
			ds.setTestOnBorrow(false);
			ds.setTestOnReturn(false);
	
			//配置监控统计拦截的filters
			ds.setFilters("stat");
			//超过时间限制是否回收
			ds.setRemoveAbandoned(true);
			//超时时间；单位为秒。180秒=3分钟
			ds.setRemoveAbandonedTimeout(1800);
			//关闭abanded连接时输出错误日志
			ds.setLogAbandoned(true);
			return ds;
		}catch(SQLException e){
			log.error("数据库连接失败：", e);
		}
		return null;
	}

	@Bean(name = "viewSqlSessionFactory")
	public SqlSessionFactory viewSqlSessionFactory() throws Exception
	{
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(viewDataSource());
		factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ViewDataSourceConfig.MAPPER_LOCATION));
		// 关闭日志
		org.apache.ibatis.session.Configuration ibatisConfiguration = new org.apache.ibatis.session.Configuration();
		ibatisConfiguration.setLogImpl(NoLoggingImpl.class);
	    factoryBean.setConfiguration(ibatisConfiguration);
		//分页插件
		PageInterceptor pageHelper = viewPageInterceptor();
		//mybatis插件
		factoryBean.setPlugins(new Interceptor[]{ pageHelper});
		return factoryBean.getObject();

	}
	
	@Bean
	public PageInterceptor viewPageInterceptor()
	{
		PageInterceptor pageInterceptor = new PageInterceptor();
		Properties p = new Properties();
		p.setProperty("offsetAsPageNum", "true");
		p.setProperty("rowBoundsWithCount", "true");
		p.setProperty("reasonable", "true");
		pageInterceptor.setProperties(p);
		return pageInterceptor;
	}

	@Bean(name = "viewSqlSessionTemplate")
	public SqlSessionTemplate viewSqlSessionTemplate() throws Exception
	{
		return new SqlSessionTemplate(viewSqlSessionFactory());
	}
	
	@Bean(name = "viewJdbcTemplate")
	public JdbcTemplate getJdbcTemplate(@Qualifier("viewDataSource") DataSource dataSource)
	{
		return new JdbcTemplate(dataSource);
	}
	
	@Bean(name = "viewTransactionManager")
	public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("viewDataSource") DataSource dataSource)
	{
		return new DataSourceTransactionManager(dataSource);
	}
}

